﻿Imports System
Imports System.Collections
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.ComponentModel.Serialization
    Friend Class ActivityCollectionMarkupSerializer
        Inherits CollectionMarkupSerializer
        ' Methods
        Protected Friend Overrides Sub AddChild(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object, ByVal childObj As Object)
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            If (childObj Is Nothing) Then
                Throw New ArgumentNullException("childObj")
            End If
            Dim activitys As ActivityCollection = [TryCast](obj,ActivityCollection)
            If (activitys Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_SerializerTypeMismatch", New Object() { GetType(ActivityCollection).FullName }), "obj")
            End If
            If Not TypeOf childObj Is Activity Then
                Throw New InvalidOperationException(SR.GetString("Error_ActivityCollectionSerializer", New Object() { childObj.GetType.FullName }))
            End If
            Dim owner As CompositeActivity = [TryCast](activitys.Owner,CompositeActivity)
            If (Not owner Is Nothing) Then
                If Helpers.IsCustomActivity(owner) Then
                    Throw New InvalidOperationException(SR.GetString("Error_CanNotAddActivityInBlackBoxActivity"))
                End If
                MyBase.AddChild(serializationManager, obj, childObj)
            End If
        End Sub

        Protected Friend Overrides Sub ClearChildren(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object)
        End Sub

        Protected Friend Overrides Function GetChildren(ByVal serializationManager As WorkflowMarkupSerializationManager, ByVal obj As Object) As IList
            If (obj Is Nothing) Then
                Throw New ArgumentNullException("obj")
            End If
            Dim activitys As ActivityCollection = [TryCast](obj,ActivityCollection)
            If (activitys Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_SerializerTypeMismatch", New Object() { GetType(ActivityCollection).FullName }), "obj")
            End If
            Dim owner As CompositeActivity = [TryCast](activitys.Owner,CompositeActivity)
            If ((Not owner Is Nothing) AndAlso Helpers.IsCustomActivity(owner)) Then
                Return Nothing
            End If
            Return MyBase.GetChildren(serializationManager, obj)
        End Function

    End Class
End Namespace

